/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.restaumanager.service.impl;

import com.restaumanager.api.IFiltreForm;
import com.restaumanager.dao.IDAO;
import com.restaumanager.service.IService;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Query;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Fabrice
 */
@Transactional
public abstract class DefaultService<T, PK extends java.io.Serializable> implements IService<T, PK> {

    public abstract IDAO<T, PK> getDao();

    public synchronized T ajouter(T t) {
//        try {
            return this.getDao().ajouter(t);
//        } catch (Exception ex) {
//            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
//            return null;
//        }
    }

    public synchronized T modifier(T t) {
//        try {
            return this.getDao().modifier(t);
//        } catch (Exception ex) {
//            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
//            return t;
//        }
    }

    public synchronized boolean supprimer(PK k) {
//        try {
            return this.getDao().supprimer(k);
//        } catch (Exception ex) {
//            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
//            return false;
//        }
    }

    public synchronized boolean supprimer(T t) {
        try {
            this.getDao().supprimer(t);
            return true;
        } catch (Exception ex) {
            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public T selectionner(PK k) {
        try {
            return (T) this.getDao().selectionner(k);
        } catch (Exception ex) {
            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public List<T> lister() {
        try {
            return this.getDao().lister();
        } catch (Exception ex) {
            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            return Collections.EMPTY_LIST;
        }
    }

    public List<T> lister(String tri, boolean asc) {
        try {
            return this.getDao().lister(tri, asc);
        } catch (Exception ex) {
            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            return Collections.EMPTY_LIST;
        }
    }

    public Long nombreEnreg() {
        try {
            return this.getDao().nombreEnreg();
        } catch (Exception ex) {
            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            return 0l;
        }
    }

    public Long nombreEnreg(IFiltreForm q) {
        try {
            return this.getDao().nombreEnreg(q);
        } catch (Exception ex) {
            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            return 0l;
        }
    }

    public List<T> lister(String tri) {
        try {
            return this.getDao().lister(tri, true);
        } catch (Exception ex) {
            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            return Collections.EMPTY_LIST;
        }
    }

    public List<T> lister(int debut, int nombre, String tri, boolean asc) {
        try {
            return this.getDao().lister(debut, nombre, tri, asc);
        } catch (Exception ex) {
            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            return Collections.EMPTY_LIST;
        }
    }

    public List<T> lister(int debut, int nombre, IFiltreForm q) {
        try {
            return this.getDao().lister(debut, nombre, q);
        } catch (Exception ex) {
            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            return Collections.EMPTY_LIST;
        }
    }

    public List<T> lister(IFiltreForm q, String sortAttrib, String sortDir) {
        try {
            return this.getDao().executerRequete(q, sortAttrib, sortDir);
        } catch (Exception ex) {
            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            return Collections.EMPTY_LIST;
        }
    }

    public void supprimer(PK[] pks) {
        for (int i = 0; i < pks.length; i++) {
            try {
                this.getDao().supprimer(pks[i]);
            } catch (Exception ex) {
                Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * Voir {@link IService#supprimer(java.util.Collection) }
     *
     */
    public void supprimer(Collection<T> ts) {
        this.supprimer(ts.iterator());
    }

    /**
     * Voir {@link IService#supprimer(java.util.Iterator) }
     *
     */
    public void supprimer(Iterator<T> ts) {
        while (ts.hasNext()) {
            try {
                this.getDao().supprimer(ts.next());
            } catch (Exception ex) {
                Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * Voir {@link IService#ajouter(java.util.Collection) }
     *
     */
    @Override
    public Collection<T> ajouter(Collection<T> ts) {
        return this.ajouter(ts.iterator());
    }

    /**
     * Voir {@link IService#ajouter(java.util.Iterator) }
     *
     */
    public Collection<T> ajouter(Iterator<T> ts) {
        Collection<T> ts2 = new LinkedList<T>();
        while (ts.hasNext()) {
            try {
                ts2.add(this.getDao().ajouter(ts.next()));
            } catch (Exception ex) {
                Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return ts2;
    }

    /**
     * Voir {@link IService#modifier(java.util.Collection) }
     *
     */
    public Collection<T> modifier(Collection<T> ts) {
        return this.modifier(ts.iterator());
    }

    /**
     * Voir {@link IService#modifier(java.util.Iterator) }
     *
     */
    public Collection<T> modifier(Iterator<T> ts) {
        Collection<T> ts2 = new LinkedList<T>();
        while (ts.hasNext()) {
            try {
                ts2.add(this.getDao().modifier(ts.next()));
            } catch (Exception ex) {
                Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return ts2;
    }

    public void supprimer() {
        try {
            this.getDao().supprimer();
        } catch (Exception ex) {
            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Voir {@link IService#exist(java.io.Serializable) }
     *
     */
    public boolean exist(PK k) {
        try {
            return this.getDao().selectionner(k) != null;
        } catch (Exception ex) {
            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public Query creerQuery(String requete) {
        try {
            return this.getDao().creerQuery(requete);
        } catch (Exception ex) {
            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public List<T> lister(int debut, int nombre, String tri, boolean asc, IFiltreForm filtre) {
        try {
            return this.getDao().lister(debut, nombre, tri, asc, filtre);
        } catch (Exception ex) {
            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            return Collections.EMPTY_LIST;
        }
    }
    
    public List<T> lister(IFiltreForm q) {
        try {
            return this.getDao().executerRequete(q, null, null);
        } catch (Exception ex) {
            Logger.getLogger(DefaultService.class.getName()).log(Level.SEVERE, null, ex);
            return Collections.EMPTY_LIST;
        }
    }
    
}
